package com.blockmeta.bbs.immodule.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.activity.chat.UUChatActivity;
import com.blockmeta.bbs.immodule.adapter.ContactsFriendsAdapter;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.bean.beaninterface.IFriendsEntry;
import com.blockmeta.bbs.immodule.bean.sort.PinyinComparator;
import com.blockmeta.bbs.immodule.net.api.TemporaryFriendApi;
import com.blockmeta.bbs.immodule.net.pojo.TemporaryFriendsResponse;
import com.blockmeta.bbs.immodule.utils.HanziToPinyin;
import com.blockmeta.bbs.immodule.widget.SideBar;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * @author airthor
 * @date 2019/3/14
 * 临时会话好友
 */
public class TemporaryFriendActivity extends BaseActivity implements SideBar.OnTouchingLetterChangedListener{
    @BindView(R2.id.rcv_friends)
    RecyclerView mRcvFriends;
    @BindView(R2.id.group_dialog)
    TextView mGroupDialog;
    @BindView(R2.id.sidebar)
    SideBar mSidebar;


    private List<IFriendsEntry> mFriendsEntries = new ArrayList<>();
    public ContactsFriendsAdapter mFriendsAdapter;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_temporary_friend,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        setTitleText("临时会话");

        mSidebar.setOnTouchingLetterChangedListener(this);
        mSidebar.bringToFront();
        mSidebar.setTextView(mGroupDialog);

        mRcvFriends.setLayoutManager(new LinearLayoutManager(this));
        mFriendsAdapter = new ContactsFriendsAdapter(R.layout.item_contacts_friends,mFriendsEntries);
        mFriendsAdapter.bindToRecyclerView(mRcvFriends);
        mFriendsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                IFriendsEntry iFriendsEntry = mFriendsEntries.get(position);
                if(iFriendsEntry != null) {
                    startActivity(new Intent(TemporaryFriendActivity.this, UUChatActivity.class)
                            .putExtra(Params.USER_TAG, iFriendsEntry.getUserId()));
                }
            }
        });

        if (mFriendsAdapter.getEmptyView()==null) {
            View emptyView = getLayoutInflater().inflate(R.layout.empty_login, null);
            ImageView icon = emptyView.findViewById(R.id.iv_icon);
            icon.setImageResource(R.drawable.icon_empty_list);
            TextView textView = emptyView.findViewById(R.id.empty_chat);
            textView.setText("暂无临时会话");
            mFriendsAdapter.setEmptyView(emptyView);
        }

        netWork();
    }

    private void netWork() {
        RetrofitFactory.getRetrofit().create(TemporaryFriendApi.class)
                .getTemporaryFriend(AccountHelper.getUidInt())
                .compose(RxUtil.<BaseImResponse<List<TemporaryFriendsResponse>>>rxSchedulerHelper())
                .compose(RxUtil.<List<TemporaryFriendsResponse>>commonResult())
                .compose(this.<List<TemporaryFriendsResponse>>bindToLifecycle())
                .subscribe(new CommonSubscriber<List<TemporaryFriendsResponse>>(mContext) {
                    @Override
                    public void onNext(List<TemporaryFriendsResponse> responses) {
                        for (TemporaryFriendsResponse response : responses) {
                            response.letter = HanziToPinyin.getInstance().getLetter(response.getName());
                        }
                        mFriendsEntries.addAll(responses);
                        Collections.sort(mFriendsEntries,new PinyinComparator());
                        mFriendsAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onTouchingLetterChanged(String s) {
        mFriendsAdapter.scrollToTargetLetter(s);
    }
}
